package com.audaque.springboot.foshanupload.web.datamongodbdemo.controller;


import com.audaque.springboot.foshanupload.web.datamongodbdemo.repository.UserRepository;
import com.audaque.springboot.foshanupload.web.datamongodbmodeldemo.model.pojo.Person;
import com.audaque.springboot.foshanupload.web.datamongodbmodeldemo.model.pojo.QUser;
import com.audaque.springboot.foshanupload.web.datamongodbmodeldemo.model.pojo.User;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import com.querydsl.mongodb.morphia.MorphiaQuery;
import org.mongodb.morphia.Morphia;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.querydsl.QPageRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/userQuerydsl")
public class UserQuerydslController {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("/findAll")
    public List<User> findAll() {
        QUser ql = QUser.user;
        Predicate pre = ql.name.eq("5");
        List<User> list = (List<User>) userRepository.findAll(pre);
        return list;
    }
    //jpa做分页查询时，分页从0开始。
    @GetMapping("/page")
    public Page<User> page() {
        BooleanBuilder builder = new BooleanBuilder();
        QUser user = QUser.user;
        builder.and(user.name.eq("5").or(user.username.eq("55")));
        Page<User> page = userRepository.findAll(builder, new QPageRequest(0,1));
        return page;
    }

}
